/**
 * @Title: AddressController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/24 17:13
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.artemis.framework.service.message.utils.MobilePhoneUtil;
import cn.com.yiqi.wsc.Ro.AddressVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.customer.impl.CustomerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *
 *@ClassName: AddressController
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/24 17:13
 *
 */
@ResponseBody
@RequestMapping("/api/address")
@Controller
public class ApiAddressController {

    @Autowired
    CustomerService customerService;

    /**
     * 收货地址列表
     * */
    @RequestMapping("/list")
    public Result list(@RequestBody CommonRO ro){
        Result re = new Result();
        List<AddressVO> address = customerService.findAllAddress(ro.getUserId());
        re.putData("list",address);
        return ResultUtil.success(re);
    }

    /**
     * 收货地址编辑页面
     * */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CommonRO ro){
        Result re = new Result();
        AddressVO vo = customerService.findaddressByid(ro.getId());
        re.putData("address",vo);
        return re;
    }

    /**
     *设为默认
     *
     */
    @RequestMapping("/setDefault")
    public Result setDefault(@RequestBody CommonRO ro){
        customerService.setDefaultAddress(ro.getId());
        return ResultUtil.success(new Result());
    }

    /**
     * 跟新收货地址
     * */
    @RequestMapping("/updateAddress")
    public Result updateAddress(@RequestBody AddressVO vo){
        Result re = new Result();
        if(vo == null || vo.getId() == null){
            return ResultUtil.fail(re,"id不能为空");
        }
        if(!ResultUtil.isSuccess(validateAddressBase(vo))){
            return re;
        }
        customerService.updateAddress(vo);
        return ResultUtil.success(re);
    }

    /**
     * 新增收货地址
     * */
    @RequestMapping("/saveAddress")
    public Result saveAddress(@RequestBody AddressVO vo){
        Result re = new Result();
        if(!ResultUtil.isSuccess(validateAddressBase(vo))){
            return re;
        }
        customerService.insertAddress(vo);
        return ResultUtil.success(re);
    }

    private Result validateAddressBase(AddressVO vo){
        Result re= new Result();
        if(vo ==null || vo.getCustomerId() == null){
            return ResultUtil.fail(re,"用户id不能为空！");
        }
        if(StringUtils.isEmpty(vo.getCityCode()) || vo.getCityCode().length() != 6){
            return ResultUtil.fail(re,"地址必填！");
        }
        if(StringUtils.isEmpty(vo.getLinkTel()) || !MobilePhoneUtil.isVaildAdress(vo.getLinkTel())){
            return ResultUtil.fail(re,"联系电话不能为空或者号码格式错误！");
        }
        if(StringUtils.isEmpty(vo.getLinkName())){
            return ResultUtil.fail(re,"联系人不能为空！");
        }
        if(StringUtils.isEmpty(vo.getAddress())){
            return ResultUtil.fail(re,"详细地址不能空！");
        }
        return ResultUtil.success(re);
    }

    /**
     * 删除地址
     * */
    @RequestMapping("/delete")
    public Result delete(@RequestBody CommonRO ro){
        Result re= new Result();
        if(null == ro.getId()){
            return ResultUtil.fail(re,"id不能为空");
        }
        customerService.deleteAddress(ro.getId());
        return ResultUtil.success(re);
    }

}
